package com.rongke.web.quartz;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.model.SysParams;
import com.rongke.service.SysParamsService;
import com.rongke.web.gubo.RoyaltyApi;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;

import java.io.IOException;

/**
 * Created by Administrator on 2017/8/7 0007.
 */
@Component("GetTokenJob")
@Transactional
@CrossOrigin
public class GetTokenJob {
    private Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private SysParamsService sysParamsService;

    public void printMessage()  {
        log.info("每七天获取一次token");
        try {
            JSONObject result = RoyaltyApi.getToken();
            if(result.getString("code").equals("000000")){
                //成功将token存入表中
                EntityWrapper<SysParams> ew = new EntityWrapper<>();
                ew.eq("sys_key","royaltyToken");
                SysParams sysParams = sysParamsService.selectOne(ew);
                sysParams.setSysValue(result.getString("data"));
                sysParamsService.updateById(sysParams);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
